<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="品牌名称" prop="brandName">
                    <el-input v-model="formData.brandName" placeholder="请输入品牌名称" />
                </el-form-item>
                <el-form-item label="使用状态" prop="status">
                    <el-radio-group v-model="formData.status" placeholder="请选择使用状态 0无效，1有效">
                        <el-radio label="0">无效</el-radio>
                        <el-radio label="1">有效</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="创建者id" prop="createId">
                    <el-input v-model="formData.createId" placeholder="请输入创建者id" />   
                </el-form-item>
                <el-form-item label="创建者" prop="createName">
                    <el-input v-model="formData.createName" placeholder="请输入创建者" />
                </el-form-item>
                <el-form-item label="评估价格" prop="evaluateMoney">
                    <el-input v-model="formData.evaluateMoney" placeholder="请输入评估价格" />
                </el-form-item>
                <el-form-item label="区分类型" prop="type">
                    <el-select class="flex-1" v-model="formData.type" placeholder="请选择区分类型">
                        <el-option label="回收" value="recycle" />
                        <el-option label="鉴别" value="evaluate" />
                    </el-select>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  brandEdit, brandAdd, brandDetail } from '@/api/brand'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑品牌管理' : '新增品牌管理'
})

const formData = reactive({
    id: '',
    brandName: '',
    status: '',
    createId: '1',
    createName: 'admin',
    evaluateMoney: '',
    type: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入主键编号',
            trigger: ['blur']
        }
    ],
    brandName: [
        {
            required: true,
            message: '请输入品牌名称',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择使用状态 0无效，1有效',
            trigger: ['blur']
        }
    ],
    createId: [
        {
            required: true,
            message: '请输入创建者id',
            trigger: ['blur']
        }
    ],
    createName: [
        {
            required: true,
            message: '请输入创建者',
            trigger: ['blur']
        }
    ],
    evaluateMoney: [
        {
            required: true,
            message: '请输入评估价格',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择区分类型:recycle回收，evaluate鉴别',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await brandEdit(data) : await brandAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await brandDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
